#pragma once

#include <unordered_map>
#include <chrono>

namespace frame {
namespace imp {

class ServerChannelImp;

class HeartBeatChannelMaps  {
public:
    struct ChannelNode {
        ChannelNode(ServerChannelImp * channel, uint64_t maxHeartBeatReadTimespan, uint64_t maxHeartBeatWriteTimespan);

        std::chrono::steady_clock::time_point now();

        void checkReadTimestamp();
        void checkWriteTimestamp();

        void updateLastReadTimestamp(){
            lastReadTimestamp = now();
        }
        void updateLastWriteTimestamp(){
            lastWriteTimestamp = now();
        }

        ServerChannelImp * channel;
        uint64_t maxHeartBeatReadTimespan;
        uint64_t maxHeartBeatWriteTimespan;

        std::chrono::steady_clock::time_point  lastReadTimestamp;
        std::chrono::steady_clock::time_point lastWriteTimestamp;
    };


    bool addChannel(uint64_t channelId, uint64_t maxHeartBeatReadTimespan, uint64_t maxHeartBeatWriteTimespan, ServerChannelImp * channelImp);
    bool removeChannel(uint64_t channelId);
    ChannelNode * getChannel(uint64_t channelId);

    typedef std::unordered_map<uint64_t, ChannelNode*> ChannelMap;
    typedef ChannelMap::const_iterator const_iterator;
    const_iterator begin() const {
        return maps.cbegin();
    }
    const_iterator end() const {
        return maps.cend();
    }
    
private:
    std::unordered_map<uint64_t, ChannelNode*> maps;
};

} //end of namespace  imp
} //end of namespace  frame
